package com.android.clinic.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.android.clinic.app.R;
import com.android.clinic.app.entity.DynamicInfo;
import com.android.clinic.app.ui.doctor.ListAllDoctorActivity;
import com.android.clinic.app.ui.widget.VerticalRollingTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2018/2/8.
 */
public class HomeDynamicAdapter extends BaseDelegateAdapter {

    private Activity activity;

    private int count = 0;
    private int currentPos = 0;
    private List<DynamicInfo> lists = new ArrayList<>();
    private ViewHolder viewHolder;
    private boolean isUpdate = false;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                currentPos = count % lists.size();
                viewHolder.rollingView.next();
                viewHolder.rollingView.setText(lists.get(currentPos).getTitle());
                viewHolder.rollingView.setOnClickListener(view -> {
                   // 事件处理

                });
                count++;
                handler.sendEmptyMessageDelayed(0, 3000);
            }
        }
    };

    public HomeDynamicAdapter(Context context, int... resource) {
        super(context, R.layout.item_home_dynamic);
        activity = (Activity) context;
    }

    @Override
    protected Object setupViewHolder(@Nullable View convertView, ViewGroup parent, int typeIndex) {
        return new ViewHolder(convertView);
    }

    @Override
    protected void setupBindViewHolder(Object holder, int position, int typeIndex) {
        if (lists.size() != 0) {
            viewHolder = (ViewHolder) holder;
            if(isUpdate){
                isUpdate = false;
                handler.removeMessages(0);
                handler.sendEmptyMessage(0);
            }
        }

        viewHolder.layMore.setOnClickListener(v -> activity.startActivity(
                new Intent(activity, ListAllDoctorActivity.class)));
    }

    @Override
    public int getItemCount() {
        if (lists != null && lists.size() > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    public void setArrayList(ArrayList<DynamicInfo> dynamicInfoList) {
        if (dynamicInfoList != null && dynamicInfoList.size() > 0) {
            this.lists = dynamicInfoList;
            this.isUpdate = true;
            handler.removeMessages(0);
            notifyDataSetChanged();
        }
    }

    class ViewHolder extends BaseViewHolder {

        VerticalRollingTextView rollingView;
        LinearLayout layMore;

        public ViewHolder(View itemView) {
            super(itemView);
            rollingView = (VerticalRollingTextView) itemView.findViewById(R.id.rollingView);
            layMore = (LinearLayout) itemView.findViewById(R.id.layout_more);
        }
    }
}
